package com.iplant.ams.controller.dms.maintenance;

import com.iplant.ams.service.DRMService;
import com.iplant.ams.service.po.dms.maintenance.DMSMaintenancePlan;
import com.iplant.ams.service.po.dms.maintenance.DMSMaintenanceTask;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/DMSMaintenanceTask")
public class DMSMaintenanceTaskController extends BasicController<DMSMaintenanceTask> {
    private static Logger logger = LoggerFactory.getLogger(DMSMaintenanceTaskController.class);

    @Autowired
    DRMService mDRMService;

    public DMSMaintenanceTaskController() {
        super(DMSMaintenanceTask.class);
    }

    /**
     * 查询维保计划
     */
    @GetMapping("/QueryDMSMaintenancePlanList")
    public Object QueryDMSMaintenancePlanList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            String wCode = StringUtils.parseString(request.getParameter("Code"));
            String wName = StringUtils.parseString(request.getParameter("Name"));
            int wStatus = StringUtils.parseInt(request.getParameter("Status"));
            int wFactoryID = StringUtils.parseInt(request.getParameter("FactoryID"));
            int wWorkShopID = StringUtils.parseInt(request.getParameter("WorkShopID"));
            int wWorkAreaID = StringUtils.parseInt(request.getParameter("WorkAreaID"));
            int wType = StringUtils.parseInt(request.getParameter("Type"), 1);

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<DMSMaintenancePlan>> wServiceResult =
                    mDRMService.DMS_QueryDMSMaintenancePlanList(wLoginUser, wCode, wName, wStatus, wFactoryID,
                            wWorkShopID, wWorkAreaID, wType, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }
}
